package com.luck.easyfoodbatch.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luck.easyfoodbatch.R;
import com.luck.easyfoodbatch.db.MessageDB;
import com.luck.easyfoodbatch.entity.Message;
import com.luck.easyfoodbatch.ui.OrderMessageActivity;
import com.luck.easyfoodbatch.ui.SystemActivity;
import com.luck.easyfoodbatch.util.Tools;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author：luck
 * project：EasyFoodBatch
 * package：com.luck.easyfoodbatch.fragment
 * email：893855882@qq.com
 * data：16/9/27
 */
public class SystemMessageFragment extends BaseFragment {

    @BindView(R.id.rl_system)
    RelativeLayout rlSystem;
    @BindView(R.id.rl_order)
    RelativeLayout rlOrder;
    @BindView(R.id.tv_system_time)
    TextView tv_system_time;
    @BindView(R.id.tv_order_time)
    TextView tv_order_time;
    @BindView(R.id.tv_system_dec)
    TextView tv_system_dec;
    @BindView(R.id.tv_order_dec)
    TextView tv_order_dec;
    @BindView(R.id.unread_msg_number1)
    TextView unreadMsgNumber1;
    @BindView(R.id.unread_msg_number2)
    TextView unreadMsgNumber2;
    private View rootView;
    private MessageDB messageDB;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fc_system, container, false);
        }
        messageDB = new MessageDB(getActivity());
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @OnClick({R.id.rl_system, R.id.rl_order})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_system:
                startAct(SystemActivity.class);
                break;
            case R.id.rl_order:
                startAct(OrderMessageActivity.class);
                break;
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        if (isCheckedLogin()) {
            initMessage();
        }
    }

    /**
     * 消息设置
     */
    private void initMessage() {
        setNewMessage();
        Message message1 = messageDB.getByUidMessage("1");
        if (message1 != null) {
            tv_system_dec.setText(message1.getContent());
            tv_system_time.setText(Tools.SubTime3(message1.getCreated_at() + ""));
        } else {
            tv_system_dec.setVisibility(View.GONE);
            tv_system_time.setVisibility(View.GONE);
        }
        Message message2 = messageDB.getByUidMessage("2");
        if (message2 != null) {
            tv_order_dec.setText(message2.getContent());
            tv_order_time.setText(Tools.SubTime3(message2.getCreated_at() + ""));
        } else {
            tv_order_dec.setVisibility(View.GONE);
            tv_order_time.setVisibility(View.GONE);
        }
    }

    /**
     * 设置新的未读消息数量
     */
    private void setNewMessage() {
        int systemMsgCount = messageDB.getByTypeMessagesCount("1"); // 系统消息
        int orderMsgCount = messageDB.getByTypeMessagesCount("2"); // 订单消息
        if (systemMsgCount > 0) {
            unreadMsgNumber1.setVisibility(View.VISIBLE);
            if (systemMsgCount > 99) {
                unreadMsgNumber1.setText("99");
            } else {
                unreadMsgNumber1.setText(systemMsgCount + "");
            }
        } else {
            unreadMsgNumber1.setVisibility(View.INVISIBLE);
        }

        if (orderMsgCount > 0) {
            unreadMsgNumber2.setVisibility(View.VISIBLE);
            if (orderMsgCount > 99) {
                unreadMsgNumber2.setText("99");
            } else {
                unreadMsgNumber2.setText(orderMsgCount + "");
            }
        } else {
            unreadMsgNumber2.setVisibility(View.INVISIBLE);
        }
    }
}
